package servlet.rool;

import pojo.Commodity;
import service.CommodityClassService;
import service.CommodityService;
import service.impl.CommodityClassServiceImpl;
import service.impl.CommodityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 前台页面搜索功能的servlet
 */
@WebServlet(value = "/FindServlet")
public class FindServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value=request.getParameter("A");
        CommodityClassService ccs=new CommodityClassServiceImpl();
        List commodities=ccs.queryCommodityClassByKey(value);
        if(commodities.size()!=0){
            request.setAttribute("commodities",commodities);
            request.getRequestDispatcher("QueryComm.jsp").forward(request,response);
        } else {
            CommodityService cs=new CommodityServiceImpl();
            List<Commodity> commodities1=cs.queryCommoditiesByValue(value);
            request.setAttribute("commodities",commodities1);
            request.getRequestDispatcher("QueryComm.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
